package com.erick.contacts.activity;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import com.erick.R;
import com.erick.contacts.helper.DBHelper;

/**
 * @Title: ContactsAdd.java
 * @Package com.erick.contacts.activity
 * @company： 科贸学院
 * @Description: 新增联系人
 * @author ChenBin
 * @date 2012-9-27 下午1:41:33
 * @version V1.0
 */
public class ContactsAddActivity extends Activity {

	private EditText nameEdit;
	private EditText phoneEdit;
	private EditText emailEdit;
	private String nameText = null;
	private String phoneText = null;
	private String emailText = null;
	private DBHelper helper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.contacts_add);

		initUI();

	}

	private void initUI() {
		nameEdit = (EditText) findViewById(R.id.nameEdit);
		phoneEdit = (EditText) findViewById(R.id.phoneEdit);
		emailEdit = (EditText) findViewById(R.id.emailEdit);
	}

	public void onClickListener(View v) {
		int viewId = v.getId();
		switch (viewId) {
		case R.id.contacts_back:
			ContactsAddActivity.this.finish();
			break;

		case R.id.contacts_confirm:
			nameText = nameEdit.getText().toString();
			phoneText = phoneEdit.getText().toString();
			emailText = emailEdit.getText().toString();
			if ("".equals(nameText)) {
				Toast.makeText(ContactsAddActivity.this, "姓名不能为空", 0).show();
			} else if ("".equals(phoneEdit)) {
				Toast.makeText(ContactsAddActivity.this, "手机号码不能为空", 0).show();
			} else if ("".equals(emailEdit)) {
				Toast.makeText(ContactsAddActivity.this, "邮箱不能为空", 0).show();
			} else {
				addData();
			}
		}
	}
	
	private void addData() {
		ContentValues values = new ContentValues();
		helper = new DBHelper(this);
		values.put("name", nameText);
		values.put("phone", phoneText);
		values.put("email", emailText);
		
		SQLiteDatabase db = helper.getWritableDatabase();
		db.insert("contact_list",null, values);
		if(db != null) {
			db.close();
		}
		ContactsAddActivity.this.startActivity(new Intent(ContactsAddActivity.this, ContactsListActivity.class));
		ContactsAddActivity.this.finish();
	}
}
